<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Exceptions\InvalidRequestException;
use App\Models\Warehouse;
use App\Models\WarehousesIn;
use App\Models\WarehousesOut;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class WarehouseService
{
    public function get($search){
        $sql = Warehouse::with('project')->when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('title','like', '%'.$search.'%');
            });
        });
        return $sql;
    }

    public function getIns($search){
        $sql = WarehousesIn::with('warehouse')->with('application')->with('user')->when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('title','like', '%'.$search.'%');
            });
        });
        return $sql;
    }

    public function getOuts($search){
        $sql = WarehousesOut::with('warehouse')->with('application')->with('user')->when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('title','like', '%'.$search.'%');
            });
        });
        return $sql;
    }

    public function insert($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        try {
            $data = Warehouse::create($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        unset($res['id']);
        try {
            $data = Warehouse::where('id',$input['id'])->update($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function insInsert($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }

        try {
            DB::transaction(function () use ($res) {
                WarehousesIn::create($res);
                // Warehouse::where('id',$res['warehouse_id'])->increment('sum',$res['quantity']);
                return true;
            });
            
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function outsInsert($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        $is = Warehouse::find($res['warehouse_id']);
        if($is->sum < $res['quantity']){
            throw new InvalidRequestException('库存不足');
        }
        try {
            DB::transaction(function () use ($res,$is) {
                WarehousesOut::create($res);
                // $is->decrement('sum',$res['quantity']);
                return true;
            });
            
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function insApply($id){
        $data = WarehousesIn::find($id);
        try {
            Warehouse::where('id',$data->warehouse_id)->increment('sum',$data->quantity);
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function outsApply($id){
        $data = WarehousesOut::find($id);
        $is = Warehouse::find($data->warehouse_id);
        if($is->sum < $data->quantity){
            throw new InvalidRequestException('库存不足');
        }
        try {
            $is->decrement('sum',$data->quantity);
            
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }
}